import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common import keys
from selenium.webdriver.support import expected_conditions as EC
from datetime import time,datetime

from sqlalchemy.sql.coercions import expect

png_dir = "D:\\gao1\\UIweb\\\Png"

class Base():
    #初始化驱动
    def __init__(self,driver):
        self.driver = webdriver.Firefox()

    #打开网址
    def open_url(self,url):
        try:
            self.driver.get(url)
            print(f'成功打开URL:{url}')

        except Exception as e:
            print(f'打开URL失败:{e}')
            time = datetime.now().strftime("%Y%m%d%H%M%S")
            path = f"{png_dir}url{time}.png"
            if self.driver.save_screenshot(path):
                print(f"截图已保存至:{path}")
            else:
                print(f"截图保存失败:{e}")


    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,30,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(f"元素定位失败:{loc}")
            time = datetime.now().strftime('%Y%m%d%H%M%S')
            path = f'{png_dir}url{time}.png'
            if self.driver.save_screenshot(path):
                print(f'截图保存成功:{png_dir}')
            else:
                print('截图保存失败')